package com.zeny.coding.service.impl;


import com.zeny.coding.constants.CourseConstants;
import com.zeny.coding.entity.CourseCategory;
import com.zeny.coding.entity.CourseCategoryExample;
import com.zeny.coding.mapper.CourseCategoryMapper;
import com.zeny.coding.service.CourseCategoryService;
import com.zeny.coding.util.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <h2>课程-分类服务</h2>
 *
 * @Author Zeny
 */
@Service
@SuppressWarnings("all")
public class CourseCategoryServiceImpl implements CourseCategoryService {

    @Autowired
    private CourseCategoryMapper courseCategoryMapper;

    @Transactional
    @Override
    public void saveBatch(String courseId, List<String> categorys) {
        SnowFlake snowFlake = new SnowFlake(CourseConstants.dataCenterId, CourseConstants.machineId);
        for (String categoryId : categorys) {
            CourseCategory courseCategory = new CourseCategory();
            courseCategory.setId(String.valueOf(snowFlake.nextId()));
            courseCategory.setCourseId(courseId);
            courseCategory.setCategoryId(categoryId);
            this.courseCategoryMapper.insertSelective(courseCategory);
        }

    }

    @Transactional
    @Override
    public void deleteByCourseId(String courseId) {
        CourseCategoryExample courseCategoryExample = new CourseCategoryExample();
        courseCategoryExample.createCriteria().andCourseIdEqualTo(courseId);
        this.courseCategoryMapper.deleteByExample(courseCategoryExample);
    }

    @Override
    public List<String> findCategorysByCourseId(String courseId) {
        CourseCategoryExample courseCategoryExample = new CourseCategoryExample();
        courseCategoryExample.createCriteria().andCourseIdEqualTo(courseId);
        List<CourseCategory> courseCategoryList = this.courseCategoryMapper.selectByExample(courseCategoryExample);
        List<String> categoryIds = new ArrayList<>();
        for (CourseCategory courseCategory : courseCategoryList) {
            categoryIds.add(courseCategory.getCategoryId());
        }
        return categoryIds;
    }
}
